using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using BriansClimb.GameObjects;

namespace BriansClimb.DrawableGameObjects.GameScenes
{
    /// <summary>
    /// This is a game component that implements the Game Start Scene.
    /// </summary>
    public class StartScene : GameScene
    {        
        protected SpriteBatch spriteBatch = null;
        protected AudioManager audioManager;

        protected Texture2D walkLeft;
        protected Texture2D walkRight;
        protected Texture2D startMenu;
        protected Texture2D startMenu1;
        protected Texture2D startMenu2;
        protected Texture2D startMenu3;
        protected Texture2D startMenu4;
        protected Texture2D startMenu5;
        protected Texture2D startMenu6;
        protected Texture2D lastStartMenu;
        protected Texture2D[] startMenus = { null, null, null, null, null, null };

        protected Texture2D startBackground;        

        protected int itemNumber;

        protected Vector2 walkPosition;

        protected GameTime oldGameTime;

        protected Vector4 menuRange1;
        protected Vector4 menuRange2;
        protected Vector4 menuRange3;
        protected Vector4 menuRange4;
        protected Vector4 menuRange5;
        protected Vector4 menuRange6;
        
        protected bool clicked;
        protected bool pressed;
        protected bool inMotion = true;

        protected MouseState mouseState;

        protected KeyboardState oldKeyboardState;

        protected int walkIteration = 0;
        protected bool walkSwitch = false;
        bool first = true;
  
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="game">Main game object</param>
        /// <param name="background">Texture for background image</param>
        /// <param name="normalText">Font for text menu</param>
        /// <param name="selectedText">Font for selected text menu</param>
        public StartScene(Game game, Texture2D background, SpriteFont normalText, SpriteFont selectedText)
            : base(game)
        {
            //Gets the current sprite batch
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            audioManager = (AudioManager)game.Services.GetService(typeof(AudioManager));

            oldKeyboardState = Keyboard.GetState();
            
            startBackground = game.Content.Load<Texture2D>(".\\Textures\\startBackground");
            
            startMenu1 = game.Content.Load<Texture2D>(".\\Textures\\startMenu1");
            startMenu2 = game.Content.Load<Texture2D>(".\\Textures\\startMenu2");
            startMenu3 = game.Content.Load<Texture2D>(".\\Textures\\startMenu3");
            startMenu4 = game.Content.Load<Texture2D>(".\\Textures\\startMenu4");
            startMenu5 = game.Content.Load<Texture2D>(".\\Textures\\startMenu5");
            startMenu6 = game.Content.Load<Texture2D>(".\\Textures\\startMenu6");

            walkLeft = game.Content.Load<Texture2D>(".\\Textures\\LeftWalk");
            walkRight = game.Content.Load<Texture2D>(".\\Textures\\RightWalk");

            //These Vector4 variables are formatted as X1, X2, Y1, Y2 on a 2D axis
            menuRange1.X = 404; menuRange1.Y = 454; menuRange1.Z = 65; menuRange1.W = 405;
            menuRange2.X = 504; menuRange2.Y = 554; menuRange2.Z = 65; menuRange2.W = 705;
            menuRange3.X = 604; menuRange3.Y = 654; menuRange3.Z = 65; menuRange3.W = 585;
            menuRange4.X = 704; menuRange4.Y = 754; menuRange4.Z = 65; menuRange4.W = 325;
            menuRange5.X = 804; menuRange5.Y = 854; menuRange5.Z = 65; menuRange5.W = 325;
            menuRange6.X = 904; menuRange6.Y = 954; menuRange6.Z = 65; menuRange6.W = 200;
            
            startMenus[0] = startMenu1; startMenus[1] = startMenu2; startMenus[2] = startMenu3;
            startMenus[3] = startMenu4; startMenus[4] = startMenu5; startMenus[5] = startMenu6;

            walkPosition.Y = 800; walkPosition.X = 1060;

            startMenu = startMenu1;
        }

        /// <summary>
        /// Gets the selected menu option
        /// </summary>
        public int SelectedMenuIndex
        {
            get { return itemNumber; }
        }

        /// <summary>
        /// Gets and sets the inMotion value for the opening animation
        /// </summary>
        public bool InMotion
        {
            get { return inMotion; }
            set { inMotion = value; }
        }

        /// <summary>
        /// Checks the mouse events
        /// </summary>
        private void CheckButtonClick()
        {
            mouseState = Mouse.GetState();
            clicked = false;

            if (mouseState.LeftButton == ButtonState.Pressed)
                pressed = true;

            if (mouseState.LeftButton == ButtonState.Released && pressed)
            {
                clicked = true;
                pressed = false;
            }
        }

        /// <summary>
        /// Checks keyboard events for the start menu
        /// </summary>
        private void CheckKeyboard()
        {
            KeyboardState keyboardState = Keyboard.GetState();

            bool down, up;
            // Handle the keyboard
            down = (oldKeyboardState.IsKeyDown(Keys.Down) &&
                (keyboardState.IsKeyUp(Keys.Down)));
            up = (oldKeyboardState.IsKeyDown(Keys.Up) &&
                (keyboardState.IsKeyUp(Keys.Up)));

            if (down)
            {
                itemNumber++;                
                if (itemNumber > 5)
                {
                    itemNumber = 0;
                    startMenu = startMenus[itemNumber];
                }
                startMenu = startMenus[itemNumber];
            }
            if (up)
            {
                itemNumber--;                
                if (itemNumber == -1)
                {
                    itemNumber = 5;
                    startMenu = startMenus[itemNumber];
                }
                startMenu = startMenus[itemNumber];
            }

            oldKeyboardState = keyboardState;
        }

        /// <summary>
        /// Checks mouse position for the start menu
        /// </summary>
        private void CheckMouse()
        {
            CheckButtonClick();
            
            mouseState = Mouse.GetState();
            if (inMotion == true && clicked == true)
            {
                walkPosition.X = -200;
            }
            else
            {
                if (mouseState.Y > menuRange1.X && mouseState.Y < menuRange1.Y && mouseState.X > menuRange1.Z && mouseState.X < menuRange1.W)
                { startMenu = startMenu1; itemNumber = 0; }
                else if (mouseState.Y > menuRange2.X && mouseState.Y < menuRange2.Y && mouseState.X > menuRange2.Z && mouseState.X < menuRange2.W)
                { startMenu = startMenu2; itemNumber = 1; }
                else if (mouseState.Y > menuRange3.X && mouseState.Y < menuRange3.Y && mouseState.X > menuRange3.Z && mouseState.X < menuRange3.W)
                { startMenu = startMenu3; itemNumber = 2; }
                //else if (mouseState.Y > menuRange4.X && mouseState.Y < menuRange4.Y && mouseState.X > menuRange4.Z && mouseState.X < menuRange4.W)
                //{ startMenu = startMenu4; itemNumber = 3; }
                //else if (mouseState.Y > menuRange5.X && mouseState.Y < menuRange5.Y && mouseState.X > menuRange5.Z && mouseState.X < menuRange5.W)
                //{ startMenu = startMenu5; itemNumber = 4; }
                else if (mouseState.Y > menuRange6.X && mouseState.Y < menuRange6.Y && mouseState.X > menuRange6.Z && mouseState.X < menuRange6.W)
                { startMenu = startMenu6; itemNumber = 5; }
            }
        }

        /// <summary>
        /// Shows the Start Scene
        /// </summary>
        public override void Show()
        {
            base.Show();
        }

        /// <summary>
        /// Hides the Start Scene
        /// </summary>
        public override void Hide()
        {
            base.Hide();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            CheckMouse();

            CheckKeyboard();

            walkIteration++;
            if ((lastStartMenu != startMenu)&&(!first))
                audioManager.PlayCue("btnclick6");
            lastStartMenu = startMenu;
            
            base.Update(gameTime);
        }

        /// <summary>
        /// Handles the opening animation and start menu
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(startBackground, new Rectangle(0, 0, Game.Window.ClientBounds.Width,
                                Game.Window.ClientBounds.Height), Color.Black);
            
            spriteBatch.Draw(startBackground, new Rectangle(((int)walkPosition.X + 200),0,Game.Window.ClientBounds.Width,
                                Game.Window.ClientBounds.Height), Color.White);

            spriteBatch.Draw(startMenu, new Rectangle(((int)walkPosition.X + 200 + 65), 404, 642, 670), Color.White);

            if ((int)walkPosition.X + 200 > 0)
            {
                if (walkSwitch)
                {
                    spriteBatch.Draw(walkLeft, new Rectangle((int)walkPosition.X, (int)walkPosition.Y, 205, 220), Color.White);
                }
                else
                {
                    spriteBatch.Draw(walkRight, new Rectangle((int)walkPosition.X, (int)walkPosition.Y, 205, 220), Color.White);
                }
            }
            else
                inMotion = false;
            
            if (walkIteration >= 12)
            {
                walkSwitch = !walkSwitch;
                if ((int)walkPosition.X + 200 > 0)
                {
                    if(((int)walkPosition.X%10==0))
                        audioManager.PlayCue("squeak2");
                    walkPosition.X = walkPosition.X - 60;
                }
                walkIteration = 0;
            }
            if (first)
                first = !first;
            oldGameTime = gameTime;
            base.Draw(gameTime);
        }
    }
}
